package com.yupi.springbootinit.datasource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户数据源
 *
 */
@Service
@Slf4j
public class UserDataSource implements DataSource<UserVO> {

    @Resource
    UserService userService;

    @Override
    public Page<UserVO> doSearch(String searchText,long pageNum, long pageSize) {
        UserQueryRequest userQueryRequest = new UserQueryRequest();
        //在用户名和用户简介中搜索关键词
        userQueryRequest.setUserName(searchText);
        userQueryRequest.setUserProfile(searchText);
        userQueryRequest.setPageSize(pageSize);
        userQueryRequest.setCurrent(pageNum);
        Page<User> userPage = userService.searchFromEs(userQueryRequest);
        Page<UserVO> userVOPage = new Page<>(pageNum, pageSize, userPage.getTotal());
        //脱敏
        List<UserVO> userVOS = userService.getUserVO(userPage.getRecords());
        userVOPage.setRecords(userVOS);
        return userVOPage;
    }
}
